package com.qf.dao.impl;

import com.qf.dao.DeptDao;
import com.qf.pojo.Dept;
import com.qf.utils.DruidUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Queue;

public class DeptDaoImpl implements DeptDao {
    JdbcTemplate jdbcTemplate=new JdbcTemplate(DruidUtil.getDataSource());
    //查询所有部门信息

    @Override
    public List<Dept> findDeptAll() {
        String sql="select * from dept";
        List<Dept> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Dept.class));
        return query;
    }

    //根据名称查询

    @Override
    public List<Dept> findDeptByDname(String dname) {
        String sql="select * from dept where dname=?";
        List<Dept> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Dept.class),dname);
        return query;
    }
    //根据id查询

    @Override
    public List<Dept> findDeptById(int id) {
        String sql="select * from dept where id=?";
        List<Dept> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Dept.class),id);
        return query;
    }

    //添加数据

    @Override
    public int addDept(Dept dept) {
        String sql="insert into dept values(?,?,?)";
        int num = jdbcTemplate.update(sql,dept.getId(),dept.getDname(),dept.getLoc());
        return num;
    }

    //删除部门信息

    @Override
    public int deleteDeptBYDname(String dname) {
        String sql="delete from dept where dname=?";
        int num = jdbcTemplate.update(sql,dname);
        return num;
    }
}
